<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<style type="text/css">
			.button-group {
				width: 100px;
				margin-left: 10px;
			}
		</style>
	</head>
	</style>
	<body style="padding: 0px;">
		<div class="easyui-layout " style="width:460px;height:380px;position:relative;margin:0 auto;">
			<div data-options="region:'north'" style="height:80px;">
				<div id="p" class="easyui-panel" title="信息" style="height: 78px;">
					<table cellpadding="2" style="width: 80%;margin-left: 9%;padding-top: 10px;">
						<tr>
							<td width="20%">类型</td>
							<td width="80%">
								<input style="width: 100%;" type="text" id="type_combo" class="easyui-combobox" />
							</td>
						</tr>
					</table>
				</div>
			</div>
			<div data-options="region:'center',title:'信息'">
				<div id="dl" style="width: 100%;height:200px;"></div>
				<div style="margin-top: 20px;margin-left: 40px;">
					<input class="easyui-textbox" data-options="required:true" id="ipt" style="width:150px;"></input>
					<a href="#" class="easyui-linkbutton" onclick="SaveData()" id="add" class="button-group" style="width: 100px;">添加</a>
					<a href="#" class="easyui-linkbutton" onclick="destroy()" id="delete" class="button-group" style="width: 100px;">删除</a>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/json2.js"></script>
	<script type="text/javascript">
		var _type = getID();
		if(_type != "") {
			$('#type_combo').combobox({
				disabled: true
			});
		}
		var _listaddr;
		var _deleteaddr;
		var _saveaddr;
		var _title;
		var _id;
		var _name;

		getAddr();
		loadData();
		$(document).ready(function() {
			$("#type_combo").combobox({
				data: typeOptions,
				valueField: 'type',
				textField: 'title',
				method: 'get',
				onSelect: function(record) {
					console.log(record);
					$('#p').panel({
						title: record.title + "信息",
					});
					_type = record.type;
					getAddr()
					var _url = RemoteLinkAddress + record.list;
					$('#dl').datalist({
						url: _url,
						method: 'get',
						valueField: _id,
						textField: record.name,
						lines: true,
						checkbox: true,

					});
				}
			});
		});

		function getAddr() {
			for(var i = 0; i < typeOptions.length; i++) {
				if(typeOptions[i].type == _type) {
					_listaddr = typeOptions[i].list;
					_saveaddr = typeOptions[i].save;
					_deleteaddr = typeOptions[i].del;
					_title = typeOptions[i].title;
					_id = typeOptions[i].id;
					_name = typeOptions[i].name;
					$('#p').panel({
						title: _title + "信息",
					});
				}
			}
		}

		function loadData() {
			var _url = RemoteLinkAddress + _listaddr;
			$('#dl').datalist({
				url: _url,
				method: 'get',
				valueField: _id,
				textField: _name,
				lines: true,
				checkbox: true,
			});
			$("#type_combo").combobox({
				value: _title,
			});

		}

		function SaveData() {
			var _url = RemoteLinkAddress + _saveaddr;
			var _val = $("#ipt").val();
			var data = new Object();
			data[_name] = _val;
			if(_val != "") {
				$.post(_url, data,
					function(result) {
						console.log(result);

						if(result.msg == "success") {
							$('#dl').datalist('reload'); // reload the user data
							$("#ipt").textbox('setValue', "");
							$.messager.alert({ // show error message
								title: '成功',
								msg: '添加成功'
							});
						} else {
							$.messager.alert({ // show error message
								title: '错误',
								msg: "输入的数据已存在数据库"
							});
						}
					},
					'json');
			} else {
				$.messager.alert('Warning', '请输入数据!', 'info');
			}
		}

		function destroy() {
			var row = $('#dl').datalist('getSelected');
			if(row) {
				var _url1 = RemoteLinkAddress + _deleteaddr;
				var destroy_id = row[_id];
				var mapData = getMap();
				mapData.put(_id, destroy_id);
				$.messager.confirm('Confirm', '是否删除', function(r) {
					if(r) {
						$.post(_url1, mapData,
							function(result) {
								if(result.msg == "success") {
									$('#dl').datalist('reload'); // reload the user data
									$.messager.alert({ // show error message
										title: '成功',
										msg: '删除成功'
									});
								} else {
									$.messager.alert({ // show error message
										title: 'Error',
										msg: result.msg
									});
								}
							},
							'json');
					}
				});
			} else {
				alert("未选中数据!");
			}
		}
	</script>

</html>